import Vue from 'vue'
import wx from "weixin-js-sdk";
import reshandler from 'services/response-handler'

/* 电话救援，获取救援机构列表 */
// 电话救援，获取救援机构列表 response
function getOneKeyRescueResponse(response) {
  return Object.assign({}, response, {
    shopRescueInfos: response.shopRescueInfos || [],
    rescueDatas: response.rescueDatas || []
  })
}
// 电话救援，获取救援机构列表
export function OneKeyRescue(params, success, fail) {
  // request 
  params = Object.assign({}, params, {
    methodName: "OneKeyRescue",
  })

  fail = fail ? fail : (err, code) => {
    Vue.prototype.$.toast(err)
  }

  // invoke
  this.$.get(params)
    .then(reshandler.success(success, fail, getOneKeyRescueResponse), reshandler.error(fail))
}
/* 获取报险公司列表 */
// 获取报险公司列表 response
function getOneKeyInsuranceResponse(response) {
  return Object.assign({}, response, {
    carInsuranceDatas: response.carInsuranceDatas || [],
    insuranceDatas: response.insuranceDatas || [],
    myInsuranceDatas: response.myInsuranceDatas || []
  })
}
// 获取报险公司列表
export function OneKeyInsurance(params, success, fail) {
  // request 
  params = Object.assign({}, params, {
    methodName: "OneKeyInsurance",
  })

  fail = fail ? fail : (err, code) => {
    Vue.prototype.$.toast(err)
  }

  // invoke
  this.$.get(params)
    .then(reshandler.success(success, fail, getOneKeyInsuranceResponse), reshandler.error(fail))
}
/* 获取报险公司列表 */
// 获取报险公司列表 response
function getListInsuranceResponse(response) {
  return Object.assign({}, response, {})
}
// 获取报险公司列表
export function ListInsurance(params, success, fail) {
  // request 
  params = Object.assign({}, params, {
    methodName: "ListInsurance",
  })

  fail = fail ? fail : (err, code) => {
    Vue.prototype.$.toast(err)
  }

  // invoke
  this.$.get(params)
    .then(reshandler.success(success, fail, getListInsuranceResponse), reshandler.error(fail))
}

// wechat config service
export var WechatService = {
  // 调用微信sdk 授权
  initWechatReq(vm) {
    let signHref = location.href.split("#")[0];
    let signOpenId = vm.$.state;
    let signUrlInfo = encodeURIComponent(signHref);
    let signData = {
      clientType: '3',
      methodName: "QueryJssdkConfig",
      shopId: "",
      userId: "",
      urlInfo: signUrlInfo,
      wechatOpenId: signOpenId
    };
    return signData;
  },
  // config
  initWechatConfig(vm, signData) {
    vm.$.wxGet(signData).then(res => {
      res = res.data;
      console.log("res: ", res);
      wx.config({
        debug: true,
        appId: res.appId,
        timestamp: res.timestamp,
        nonceStr: res.nonceStr,
        signature: res.signature,
        jsApiList: ["getLocation"]
      });
    });
  }
}
// 定位service
export var LocationService = {
  // 执行微信sdk调用
  invokeWechatReq(vm) {
    return new Promise(function (resolve, reject) {
      wx.ready(function () {
        // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
        wx.getLocation({
          type: "wgs84",
          success: function (res) {
            // 创建地址解析器
            var myGeo = new BMap.Geocoder();

            // 创建点坐标
            var _point = new BMap.Point(res.longitude, res.latitude);

            // 根据车品牌获取经纬度没有或者还没有返回时
            if (!vm.point) {
              vm.point = _point;
            }

            // 解析获取省份
            myGeo.getLocation(_point, function (res) {
              var province = res.addressComponents.province.replace(/省/g, '');
              resolve(province)
            });
          }
        });
      });
    })
  }
}

